define(
	[
	appBasePath + '/scripts/module.js',
    ],	
    function(substanceModule) {
		substanceModule.factory('Demo.HttpService', [
		    '$http',
		    '$q',
		    function($http, $q) {		    	
	       	    return {	       	    	
					post : function(url, data) {
						var d = $q.defer();
		   	    		var promise = $http({
		   			   		url : url,
		   					method : "POST",
		   					data : data,
		   					headers : {
		   						'Content-Type' : 'application/json;charset=UTF-8'
		   					}
		   				});
		   	    		promise.success(function(data) {
		   	    			d.resolve(data);		   	    			
		   				});
		   	    		return d.promise;
					},
					get : function(url,block){						
						var d = $q.defer();
		   	    		var promise = $http({
		   			   		url : url,
		   					method : "get",
		   					headers : {
		   						'Content-Type' : 'application/json;charset=UTF-8'
		   					}
		   				});
		   	    		promise.success(function(data){
		   	    			d.resolve(data);		   	    			
		   				});
		   	    		return d.promise;
					},
					upload: function(url, files) {
						var d = $q.defer();
						var fd=new FormData();
						angular.forEach(files, function(f) {
					         fd.append('files', f, f.name);
					    });
						console.log(fd);
		   	    		var promise = $http({
		   			   		url : url,
		   					method : "POST",
		   					data : fd,
		   					headers : {
		   						'Content-Type' : 'multipart/form-data'
		   					}
		   				});
		   	    		promise.success(function(data) {
		   	    			d.resolve(data);		   	    			
		   				});
		   	    		return d.promise;
					},
					
					send: function(url, files, success, fail) {
		                var data = new FormData(),
		                xhr = new XMLHttpRequest();
		                xhr.onloadstart = function() {
		                };

		                xhr.onerror = function(e) {
		                };
		                xhr.onreadystatechange = function(e)
		                {
		                    if (xhr.readyState === 4 && xhr.status === 200)
		                    {
		                    	if (success) {
		                    		success(xhr.responseText);
		                    	}
		                    }
		                };

		                angular.forEach(files, function(f) {
		                	data.append('files', f, f.name);
		                });

		                xhr.open('POST', url);
		                xhr.send(data);


		            }
	       	    };
		    }
		]);
	}
);